﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.WebUI.Infrastructure;
using Zatify.Domain.Abstract;
using Zatify.WebUI.Admin.Models;
using Zatify.Domain.Data;
using Zatify.WebUI.Infrastructure.Filters;
using Zatify.WebUI.Infrastructure.Absrtact;

namespace Zatify.WebUI.Areas.Admin.Controllers
{
    [ZatifyAdminAuthorizeAttribute]
    [Utils.MyRequireHttps]
    public class CategoriesController : AdminBaseController
    {
        private IZatifyRepository repository;

        protected override AdminModules AdminModule
        {
            get
            {
                return AdminModules.Categories;
            }
        }

        public CategoriesController(IZatifyRepository repository, IZatifyAuthProvider authProvider)
        {
            this.repository = repository;
        }

        public ActionResult List()
        {
            var model = new CategoriesViewModel()
            {
                Categories = repository.Categories.Where(c => !c.ParentId.HasValue).ToList(),
                CategoriesRequests = repository.CategoryRequests.ToList()
            };

            return View(model);
        }

        public ActionResult AddCategory(AddNewCategoryViewModel model)
        {
            if (ModelState.IsValid)
                repository.CategoriesAdd(new Category()
                {
                    ParentId = (model.ParentId != 0 ? model.ParentId : null),
                    CategoryName = model.CategoryName,
                    SortOrder = model.SortOrder,
                    IsSingularSelection = model.IsSingularSelection,
                    IsRoot = model.IsRoot,
                    NotMergeable = model.NotMergeable,
                    CategoryDescription = model.CategoryDescription
                });

            return RedirectToAction("List");
        }

        public JsonResult GetCategory(int id)
        {
            var category = this.repository.Categories.First(c => c.CategoryId == id);

            return Json(new
            {
                CategoryId = category.CategoryId,
                CategoryName = category.CategoryName,
                SortOrder = category.SortOrder,
                ParentId = category.ParentId,
                IsSingularSelection = category.IsSingularSelection,
                IsRoot = category.IsRoot,
                CategoryDescription = category.CategoryDescription,
                NotMergeable = category.NotMergeable
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UpdateCategory(UpdateCategoryViewModel model)
        {
            if (ModelState.IsValid)
                repository.CategoriesUpdate(new Category()
                {
                    CategoryId = model.CategoryId,
                    ParentId = (model.ParentId != 0 ? model.ParentId : null),
                    CategoryName = model.CategoryName,
                    SortOrder = model.SortOrder,
                    IsSingularSelection = model.IsSingularSelection,
                    IsRoot = model.IsRoot,
                    NotMergeable = model.NotMergeable,
                    CategoryDescription = model.CategoryDescription
                });

            return RedirectToAction("List");
        }

        public ActionResult DeleteSelected(string categoriesIds)
        {
            if (categoriesIds != null && categoriesIds.Length > 0)
                repository.CategoriesDeleteSelected(categoriesIds);

            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult AddRequestedCategories(int[] requestedCategoriesIds, int? parentCategoryId, bool addRequestedCategories)
        {
            if (requestedCategoriesIds.Length > 0)
            {
                if (addRequestedCategories)
                    repository.CategoryRequestConfirm(requestedCategoriesIds[0], (parentCategoryId != 0 ? parentCategoryId : null));
                else
                    repository.CategoryRequestDelete(requestedCategoriesIds);
            }

            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult CategoryMove(int categoryIdToMove, int? newParentCategoryId)
        {
            this.repository.CategoryMove(categoryIdToMove, newParentCategoryId);

            return RedirectToAction("List");
        }

        public PartialViewResult GetCategoriesTree(int id)
        {
            var reslut = (from category in this.repository.CategoriesGetAll()
                          where category.ParentCategory == null && category.CategoryId != id && !category.IsSingleSelection
                          select category).ToList();

            return PartialView("Partials/CategoriesTreeControl",
                new CategoriesTreeControlViewModel() { Categories = reslut, ExcludCategoryId = id });
        }
    }
}
